package com.eclink.asset.consume.controller;

import com.eclink.asset.consume.convert.ConsumeCategoryConvert;
import com.eclink.asset.consume.domain.dao.ConsumeCategory;
import com.eclink.asset.consume.domain.dto.ConsumeCategoryDTO;
import com.eclink.asset.consume.domain.vo.ConsumeCategoryVO;
import com.eclink.asset.consume.service.ConsumeCategoryService;
import com.eclink.asset.pojo.query.AssetCategoryQuery;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 耗材分类Controller
 *
 * @author chenshixian
 * @date 2024-08-23
 */
@Api("耗材分类列表")
@RestController
@RequestMapping("/consume/category")
public class ConsumeCategoryController extends BaseController {

    @Resource
    private ConsumeCategoryService service;

    @Resource
    private ConsumeCategoryConvert convert;

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public ResponseEntity<Page<ConsumeCategory>> list(@RequestBody AssetCategoryQuery query, Pageable page) {
        List<ConsumeCategory> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("查询所有列表")
    @GetMapping("/listAll")
    public ResponseEntity<List<ConsumeCategory>> listAll() {
        List<ConsumeCategory> list = service.selectListAll();
        return ResponseEntity.ok(list);
    }

    @ApiOperation("根据父节点查询")
    @GetMapping("/getTreeNodesByParentId")
    public ResponseEntity<List<ConsumeCategory>> getTreeNodesByParentId(String parentId) {
        List<ConsumeCategory> list = service.getListByParentId(parentId);
        return ResponseEntity.ok(list);
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public ResponseEntity<String> export(AssetCategoryQuery query) {
        List<ConsumeCategory> list = service.selectList(query, null);
        ExcelUtil<ConsumeCategoryVO> util = new ExcelUtil<>(ConsumeCategoryVO.class);
        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "资产分类表数据"));
    }

    @ApiOperation("获取详细信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity<ConsumeCategory> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增")
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody ConsumeCategoryDTO assetCategory) {

        String parentNumber = assetCategory.getParentNumber();
        Integer parentId = assetCategory.getParentId();
        String number = service.getLastNumberFormat(parentId, parentNumber);
        ConsumeCategory dataObject = convert.dto2do(assetCategory);
        dataObject.setNumber(number);

        return ResponseEntity.ok(service.insert(dataObject));
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody ConsumeCategory assetCategory) {
        return ResponseEntity.ok(service.update(assetCategory));
    }

    @ApiOperation("删除")
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }
}
